<template>
	<view>
		<u-swiper :list="bannerImages" radius='0' height='200px' @click='clickBanner'></u-swiper>
		<view>
			<u-row>
				<u-col span='3'>
					<view class='icon' @click='goGt'>
						<img src='/static/auto-point.png' style='width:35px;height:35px'>
						<p class='icon-name'>快速积分</p>
					</view>
				</u-col>
				<u-col span='3'>
					<view class='icon' @click='toUrl("/pages/mine/park-info")'>
						<img src='/static/pay-car.png' style='width:35px;height:35px'>
						<p class='icon-name'>停车缴费</p>
					</view>
				</u-col>
				<u-col span='3'>
					<view class='icon' @click='toUrl("/pages/content/index?id=4")'>
						<img src='/static/new-content.png' style='width:35px;height:35px'>
						<p class='icon-name'>最新活动</p>
					</view>
				</u-col>
				<u-col span='3'>
					<view class='icon' @click='toUrl("/pages/content/index?id=3")'>
						<img src='/static/member.png' style='width:35px;height:35px'>
						<p class='icon-name'>会员权益</p>
					</view>
				</u-col>
			</u-row>
		</view>
		<view>
			<u-row>
				<u-col span='6'>
					<view class='type'>
						<u-row>
							<u-col span='6'>
								<view @click='toUrl("/pages/shop/index")'>
									<view style='color:#FF5D00'>积分商城</view>
									<view style='color:#909399;font-size:10px;'>会员兑换礼</view>
								</view>
							</u-col>
							<u-col span='6'>
								<view style='float:right'>
									<img src='/static/shop-active.png' style='width:40px;height:40px;'>
								</view>
							</u-col>
						</u-row>
					</view>
				</u-col>
				<u-col span='6'>
					<view class='type'>
						<u-row @click='toUrl("/pages/draw/index")'>
							<u-col span='6'>
								<view>
									<view style='color:#FF5D00'>幸运抽奖</view>
									<view style='color:#909399;font-size:10px;'>抽奖赢好礼</view>
								</view>
							</u-col>
							<u-col span='6'>
								<view style='float:right'>
									<img src='/static/draw.png' style='width:40px;height:40px;'>
								</view>
							</u-col>
						</u-row>
					</view>
				</u-col>
			</u-row>
		</view>
		<view style='padding:10px 0px'>
			<img src='/static/title.png' style='width:100%;height:48px' />
		</view>
		<view v-if='categories && categories.length > 0'>
			<view>
				<u-divider text="进驻商家"></u-divider>
			</view>
			<view v-for='item in categories'>
				<view class='cate'>
					<text class='category'>
						<text>{{item.category}}</text>
					</text>
				</view>
				<view class='container'>
					<u-scroll-list>
						<view v-for="(item, index) in item.stores" :key="index">
							<view
								style='padding:10px;background-color: #f3f3f3;margin:5px;text-align: center;color:#909399;border-radius: 5px;'>
								<image :src="item.logo" style='width:100px;height:100px'></image>
								<p>{{item.name}}</p>
							</view>
						</view>
					</u-scroll-list>
				</view>
			</view>
		</view>
		<view v-if='popPicture != ""'>
			<u-overlay :show="show">
				<view>
					<view style='margin-top:18vh;text-align: center;'>
						<view style='width:80%;margin:auto'>
							<u-image @click='toUrl(popPicture.link)' :src='popPicture.image' width='100%' mode='widthFix'></u-image>
						</view>
						<view style='margin:auto;padding-top:20px'>
							<u-icon name="close-circle" size='48' @click="show = false" color='#fff'></u-icon>
						</view>
					</view>
				</view>
			</u-overlay>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bannerImages: [],
				categories: [],
				canGoGt: false,
				banners: [],
				popPicture: '',
				show: false
			}
		},
		onLoad() {
			let token = uni.getStorageSync('api_token')
			if (token == '') {
				this.wxLogin()
			} else {
				this.getBanner()
				this.getStores()
			}
		},
		onShareAppMessage(res) {
			return {
				title: '名苑新天地',
				path: `/pages/index/index`,
				imageUrl: 'https://picture.jmgc.vip/bd3cb5a2-911a-4620-a038-e334c807be3a.png'
			}
		},
		onShow() {
			this.getUpdateManager()
			this.getAuthorizeInfo()
		},
		methods: {
			clickBanner(e) {
				console.log(e)
				if (this.banners[e].link) {
					uni.navigateTo({
						url: this.banners[e].link
					})
				}
			},
			getBanner() {
				this.request.get(`api/albums/1`).then((res) => {
					console.log('banner', res.data)
					this.banners = res.data.pictures
					let bannerImages = []
					for (let i of res.data.pictures) {
						bannerImages.push(i.image)
					}
					this.bannerImages = bannerImages
				})
				this.request.get(`api/albums/3`).then((res) => {
					if(res.data && res.data.pictures && res.data.pictures.length > 0){
						this.popPicture = res.data.pictures[0]
						console.log('123',this.popPicture)
						this.show = true
					}
				})
			},
			getStores() {
				this.request.get(`public/stores`, {
					'@filter': `entity.getStatus() == 1`,
					'@order': `entity.clockRange|ASC`,
					'@expands': '["entity.category"]'
				}).then((res) => {
					console.log('store', res.data)
					// 分类
					let categories = []
					let categoryIds = []
					for (let i of res.data) {
						let index = categoryIds.indexOf(i.category.id)
						if (categoryIds.length == 0 || index === -1) {
							categories.push({
								category: i.category.__toString,
								sequence: i.category.__metadata.sequence,
								stores: [i]
							})
							categoryIds.push(i.category.id)
						} else {
							categories[index].stores.push(i)
						}
					}
					categories.sort(function(a, b) {
						return (a.sequence - b.sequence)
					})
					this.categories = categories
				})
			},
			wxLogin() {
				let _this = this
				uni.login({
					success(result) {
						console.log(111, result)
						let code = result.code
						_this.request.get(`wechat/mini/login`, {
							code
						}).then((res) => {
							console.log('login', res)
							const token = res.data.token
							uni.setStorageSync('api_token', token)
							_this.getBanner()
							_this.getStores()
						})
					}
				})
			},
			toUrl(page) {
				console.log(page)
				uni.navigateTo({
					url: page
				})
			},
			showTips() {
				uni.showToast({
					title: '敬请期待！',
					icon: 'none',
					wechatOpenId: ''
				})
			},
			goGt() {
				uni.showLoading()
				var myPluginInterface = requirePlugin('myPlugin');
				this.request.get(`api/user`).then((res) => {
					this.wechatOpenId = res.data.wechatOpenId
					myPluginInterface.getLocation(this.wechatOpenId).then(res => {
						console.log('插件结果', res)
						if (res.return_code == 0) {
							console.log('可以跳转')
							const openId = this.wechatOpenId
							const mchId = '1695286548'
							myPluginInterface.getAuthStatus(openId, mchId).then(res => {
								console.log('跳转结果', res)
								uni.hideLoading()
								uni.navigateTo({
									url: `plugin://myPlugin/index?openid=${openId}&mch_id=${mchId}&member_status=1`
								})
							})
						} else {
							uni.hideLoading()
							uni.showModal({
								tiotle: '',
								content: `${res.return_msg}快速积分功能需要授权获取定位`,
								showCancel: false,
								success: () => {
									this.getAuthorizeInfo()
								}
							})
						}
					})
				})
			},
			getAuthorizeInfo(a = "scope.userLocation") { //1. uniapp弹窗弹出获取授权（地理，个人微信信息等授权信息）弹窗
				var _this = this;
				uni.authorize({
					scope: a,
					success() { //1.1 允许授权
						console.log('有授权')
					},
					fail() { //1.2 拒绝授权
						console.log('没有授权')
						uni.showModal({
							title: '温馨提示',
							content: '获取定位失败，为了给您提供更好的体验，请您点击右上角。。。，点击设置，微信信息设置选择允许,如有疑问请联系客服！',
							showCancel: false,
							confirmText: '我知道了',
							success: () => {

							}
						})
					}
				})
			},
			getUpdateManager() {
				// 检查小程序是否有新版本
				const updateManager = uni.getUpdateManager()
				console.log('update', updateManager)
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					if (res.hasUpdate) {
						// 如果有新版本，则提示用户更新
						uni.showModal({
							title: '更新提示',
							content: '发现新版本，是否更新？',
							success: function(res) {
								if (res.confirm) {
									// 用户点击确定按钮，调用uni.downloadFile()方法下载新版本
									updateManager.onUpdateReady(function() {
										uni.showModal({
											title: '更新提示',
											content: '新版本已经准备好，是否重启应用？',
											success: function(res) {
												if (res.confirm) {
													// 新的版本已经下载好，调用applyUpdate方法重启小程序并应用新版本
													updateManager.applyUpdate()
												}
											}
										})
									})
								} else if (res.cancel) {
									// 用户点击取消按钮，不做任何操作
								}
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.icon {
		text-align: center;
		align-items: center;
		padding: 30px 0px 20px;
	}

	.icon-name {
		color: #909399;
		font-size: 14px;
		margin-top: 10px;
	}

	.type {
		background-color: #002238;
		padding: 10px;
		margin: 3px 5px;
		border-radius: 5px;
		height: 40px;
	}

	.category {
		background-color: #002238;
		color: #fff;
		padding: 3px 10px;
		border-radius: 0px 80px 0px 0px;
		width: 18%;
	}

	.cate {
		border-bottom: solid #002238 1px;
	}

	.container {
		padding: 10px;
	}
</style>